package com.ruoyi.cms.controller.admin;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.cms.common.BaseController;
import com.ruoyi.cms.domain.Category;
import com.ruoyi.cms.domain.Field;
import com.ruoyi.cms.domain.Form;
import com.ruoyi.cms.exception.AdminGeneralException;
import com.ruoyi.cms.exception.CmsException;
import com.ruoyi.cms.exception.TransactionException;
import com.ruoyi.cms.service.FieldService;
import com.ruoyi.cms.service.FormService;
import com.ruoyi.cms.util.ExceptionEnum;
import com.ruoyi.cms.util.RandomUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.helper.LoginHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cms/form")
public class FormController extends BaseController {
    @Autowired
    private FormService formService;
    @Autowired
    private FieldService fieldService;

    @GetMapping("/list")
    public R<List<Form>> list() {
        return R.ok(formService.queryAll());
    }

    @GetMapping("/find/{id}")
    public R<Form> getForm(@PathVariable(name = "id") String id) {
        return R.ok(formService.queryFormById(id));
    }

    @PostMapping("/save")
    public R<String> saveForm(@RequestBody Form form) throws CmsException {
        form.setId(IdUtil.getSnowflake().nextIdStr());
        form.setCreateBy(""+LoginHelper.getUserId());
        form.setCreateTime(new Date());
        form.setCode(RandomUtil.getCharAndNumr(8));

        try {
            formService.add(form);
        } catch (TransactionException e) {
            throw new AdminGeneralException(
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(),
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getMessage(),
                e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("/update")
    public R<String> updateForm(@RequestBody Form newForm) throws CmsException {
        Form oldForm = formService.queryFormById(newForm.getId());

        newForm.setUpdateBy(""+LoginHelper.getUserId());
        newForm.setUpdateTime(new Date());

        try {
            formService.update(newForm,oldForm);
        } catch (TransactionException e) {
            throw new AdminGeneralException(
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(),
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getMessage(),
                e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("delete/{id}")
    public int deleteForm(@PathVariable(value = "id", required = false)String id) throws CmsException {
        Form form = formService.queryFormById(id);
        if(form.getType() == 0) {
            throw new RuntimeException("系统表单不允许删除！");
        }
        //删除字段
        List<Field> fields = fieldService.queryFieldByFormId(id);
        //删除表
        try {
            return formService.delete(form,fields);
        } catch (TransactionException e) {
            throw new AdminGeneralException(
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getCode(),
                ExceptionEnum.HTTP_INTERNAL_SERVER_ERROR.getMessage(),
                e.getMessage());
        }
    }
}
